package com.tmsps.ne4weixin.api;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.tmsps.ne4weixin.bean.message.send.OutBaseMessage;
import com.tmsps.ne4weixin.config.WxConfig;
import com.tmsps.ne4weixin.utils.XmlUtils;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @Title: MessageAPI.java
 * @Package: com.tmsps.ne4weixin.api
 * @Description: 被动回复用户消息API
 */
@Slf4j
public class MessageAPI extends BaseAPI {

	public MessageAPI(WxConfig config) {
		super(config);
	}
	
	/**
	 * 被动回复用户消息
	 * @param message 回复消息
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public boolean sendMessage(OutBaseMessage message, HttpServletResponse response) {
		if (StrUtil.isBlank(message.getToUserName()) || StrUtil.isBlank(message.getFromUserName())) {
			throw new IllegalArgumentException("发送方或接收方不能为空");
		}
		return sendMessage(message, message.getToUserName(), message.getFromUserName(), response);
	}
	
	/**
	 * 被动回复用户消息
	 * @param message 回复消息
	 * @param request 
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public boolean sendMessage(OutBaseMessage message, HttpServletRequest request, HttpServletResponse response) {
		Map<String, String> parseRequest = null;
		try {
			parseRequest = XmlUtils.parseRequest(request.getInputStream());
		} catch (IOException e) {
			log.error("获取流异常", e);
			return false;
		}
		return sendMessage(message, parseRequest.get("FromUserName"), parseRequest.get("ToUserName"), new Date().getTime(), response);
	}
	
	/**
	 * 被动回复用户消息
	 * @param message 		回复消息
	 * @param toUserName 	接收方
	 * @param fromUserName 	发送方
	 * @param response		
	 * @return
	 * @throws IOException
	 */
	public boolean sendMessage(OutBaseMessage message, String toUserName, String fromUserName, HttpServletResponse response) {
		return sendMessage(message, toUserName, fromUserName, new Date().getTime(), response);
	}
	
	/**
	 * 被动回复用户消息
	 * @param message 		回复消息
	 * @param toUserName 	接收方
	 * @param fromUserName 	发送方
	 * @return
	 * @throws IOException
	 */
	public boolean sendMessage(OutBaseMessage message, String toUserName, String fromUserName, Long createTime, HttpServletResponse response) {
		message.setFromUserName(fromUserName);
		message.setToUserName(toUserName);
		String resultXml = XmlUtils.toXml(message);
		log.debug("response message{}", resultXml);
		return responseMsg(resultXml, response);
	}
	
	private boolean responseMsg(String data, HttpServletResponse response) {
		try {
			PrintWriter out = response.getWriter();
			//如果data为空则响应 success或者 空字符串
			out.print(data == null ? "success" : data);
			out.flush();
			out.close();
		} catch (Exception e) {
			log.error("Exception in sending message", e);
			return false;
		}
		return true;
	}
}
